package com.example.airoles.dto;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;
import java.util.List;
import java.util.ArrayList;

public class UserProfile {
    public String userId;
    public String nickname;
    public String email;
    public LocalDateTime createdAt;
    public LocalDateTime lastActiveAt;
    public UserPreferences preferences;
    public ConversationStats stats;
    public List<String> favoriteRoles = new ArrayList<>();
    public Map<String, Object> customData = new HashMap<>();
    
    public UserProfile() {
        this.createdAt = LocalDateTime.now();
        this.lastActiveAt = LocalDateTime.now();
        this.preferences = new UserPreferences();
        this.stats = new ConversationStats();
    }
    
    public UserProfile(String userId, String nickname) {
        this();
        this.userId = userId;
        this.nickname = nickname;
    }
    
    public static class UserPreferences {
        public String preferredLanguage = "zh";
        public String communicationStyle = "gentle"; // "direct", "gentle", "analytical"
        public int responseLength = 2; // 1=short, 2=medium, 3=long
        public boolean enableVoice = true;
        public String preferredVoice = "alloy";
        public double speechSpeed = 1.0;
        public boolean autoRecommendRoles = true;
        public boolean enableEmotionalInsights = true;
        public List<String> interests = new ArrayList<>();
        public Map<String, Object> accessibility = new HashMap<>();
        
        public UserPreferences() {
            // Default accessibility settings
            accessibility.put("highContrast", false);
            accessibility.put("largeText", false);
            accessibility.put("screenReader", false);
        }
    }
    
    public static class ConversationStats {
        public int totalConversations = 0;
        public int totalMessages = 0;
        public Map<String, Integer> roleUsage = new HashMap<>();
        public Map<String, Integer> skillUsage = new HashMap<>();
        public String mostUsedRole;
        public String mostUsedSkill;
        public LocalDateTime lastConversationAt;
        public int streakDays = 0;
        public List<String> achievements = new ArrayList<>();
        
        public void updateRoleUsage(String roleId) {
            roleUsage.put(roleId, roleUsage.getOrDefault(roleId, 0) + 1);
            updateMostUsed();
        }
        
        public void updateSkillUsage(String skill) {
            skillUsage.put(skill, skillUsage.getOrDefault(skill, 0) + 1);
            updateMostUsed();
        }
        
        private void updateMostUsed() {
            mostUsedRole = roleUsage.entrySet().stream()
                .max(Map.Entry.comparingByValue())
                .map(Map.Entry::getKey)
                .orElse(null);
                
            mostUsedSkill = skillUsage.entrySet().stream()
                .max(Map.Entry.comparingByValue())
                .map(Map.Entry::getKey)
                .orElse(null);
        }
        
        public void incrementConversation() {
            totalConversations++;
            lastConversationAt = LocalDateTime.now();
        }
        
        public void incrementMessage() {
            totalMessages++;
        }
    }
}